//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using SR = Microsoft.Research.DataLayer;

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    /// <summary>
    /// Interaction logic for NodeEditorContentView.xaml
    /// </summary>
    public partial class NodeEditorContentView : UserControl
    {
        public NodeEditorContentView()
        {
            Pushback = true;

            InitializeComponent();
        }

        private void ClusterPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            NodeCenterPanePresenter presenter = (NodeCenterPanePresenter)this.DataContext;
            if (null!= presenter)
            {
                Pushback = false;
                if (presenter.ClusterPassword != ClusterPassword.Password)
                {
                    presenter.ClusterPassword = ClusterPassword.Password;
                }
                Pushback = true;
            }
        }
        
        /// <summary>
        /// Admin password from the password box
        /// </summary>
        public string ClusterAdminPassword
        {
            get
            {
                return ClusterPassword.Password;
            }
            set
            {
                ClusterPassword.Password = value;
            }
        }

        /// <summary>
        /// Property to control the infinite loop of setting property back and forth
        /// </summary>
        public bool Pushback
        {
            get;
            set;
        }
    }

    /// <summary>
    /// The converter to determine the visibility of the executer status.
    /// </summary>
    public class ExecutorStatusVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// Convert based on value and parameter.
        /// </summary>
        /// <param name="value">
        /// The current value.
        /// </param>
        /// <param name="targetType">
        /// The expected type.
        /// </param>
        /// <param name="parameter">
        /// Passed parameter.
        /// </param>
        /// <param name="culture">
        /// Culture.
        /// </param>
        /// <returns>
        /// Modified value.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object result = Visibility.Collapsed;
            SR.MachineStatus machineStatus = (SR.MachineStatus)value;

            if (((string)parameter).Equals("Running"))
            {
                if (machineStatus == SR.MachineStatus.Running)
                {
                    result = Visibility.Visible;
                }
                else
                {
                    result = Visibility.Collapsed;
                }
            }
            else if(((string)parameter).Equals("NotRunning"))
            {
                 if (machineStatus == SR.MachineStatus.Faulty)
                 {
                     result = Visibility.Visible;
                 }
                 else
                 {
                     result = Visibility.Collapsed;
                 }
            }
            return result;
        }

        /// <summary>
        /// Convert back based on value and parameter.
        /// </summary>
        /// <param name="value">
        /// The current value.
        /// </param>
        /// <param name="targetType">
        /// The expected type.
        /// </param>
        /// <param name="parameter">
        /// Passed parameter.
        /// </param>
        /// <param name="culture">
        /// Culture.
        /// </param>
        /// <returns>
        /// Old value.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    /// <summary>
    /// The converter to determine the visibility of the scheduler status.
    /// </summary>
    public class SchedulerStatusVisibilityConverter : IValueConverter
    {
        /// <summary>
        /// Convert based on value and parameter.
        /// </summary>
        /// <param name="value">
        /// The current value.
        /// </param>
        /// <param name="targetType">
        /// The expected type.
        /// </param>
        /// <param name="parameter">
        /// Passed parameter.
        /// </param>
        /// <param name="culture">
        /// Culture.
        /// </param>
        /// <returns>
        /// Modified value.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object result = Visibility.Collapsed;
            SR.SchedulerStatus schedulerStatus = (SR.SchedulerStatus)value;

            if (((string)parameter).Equals("Running"))
            {
                if (schedulerStatus == SR.SchedulerStatus.Running)
                {
                    result = Visibility.Visible;
                }
                else
                {
                    result = Visibility.Collapsed;
                }
            }
            else if (((string)parameter).Equals("NotRunning"))
            {
                if (schedulerStatus == SR.SchedulerStatus.Faulty)
                {
                    result = Visibility.Visible;
                }
                else
                {
                    result = Visibility.Collapsed;
                }
            }
            return result;
        }

        /// <summary>
        /// Convert back based on value and parameter.
        /// </summary>
        /// <param name="value">
        /// The current value.
        /// </param>
        /// <param name="targetType">
        /// The expected type.
        /// </param>
        /// <param name="parameter">
        /// Passed parameter.
        /// </param>
        /// <param name="culture">
        /// Culture.
        /// </param>
        /// <returns>
        /// Old value.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}
